package com.xunlai.infra.permission.business.config;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;

/**
 * @author liang
 * @date 8/16/23 11:17 PM
 */
@Configuration
@ComponentScan(basePackages = {"com.xunlai.infra.permission.business"})
@EnableJdbcRepositories(basePackages = {"com.xunlai.infra.permission.business.dao.repository"})
@EnableConfigurationProperties({QiniuAuthKey.class, WxPayConfig.class, AliyunAccessKey.class})
public class BusinessConfig {


    @Bean
    public Config wxConfig(WxPayConfig wxPayConfig){
        return new RSAAutoCertificateConfig.Builder()
                        .merchantId(wxPayConfig.merchantId())
                        .privateKeyFromPath(wxPayConfig.privateKeyPath())
                        .merchantSerialNumber(wxPayConfig.merchantSerialNumber())
                        .apiV3Key(wxPayConfig.apiV3Key())
                        .build();
    }

    @Bean
    public AsyncClient client(AliyunAccessKey accessKey){
        // Configure Credentials authentication information, including ak, secret, token
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
                .accessKeyId(accessKey.id())
                .accessKeySecret(accessKey.secret())
                //.securityToken(System.getenv("ALIBABA_CLOUD_SECURITY_TOKEN")) // use STS token
                .build());
        return AsyncClient.builder()
                //.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient)
                .credentialsProvider(provider)
                //.serviceConfiguration(Configuration.create()) // Service-level configuration
                // Client-level configuration rewrite, can set Endpoint, Http request parameters, etc.
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                        //.setConnectTimeout(Duration.ofSeconds(30))
                )
                .build();
    }
}
